#include "StateMenu.h"
#include "StateOptions.h"
#include "StatePlay.h"
#include "LoadingScreen.h"
#include "InputHelper.h"
#include "TextureHelper.h"

#include <Aurora/System/NetworkManager.h>

#include <zlib.h>
#include <dirent.h>
#include <fcntl.h>


StateMenu::StateMenu()
{

}

StateMenu::~StateMenu()
{

}

void StateMenu::Init()
{
	//set render manager instance
	mRender = RenderManager::InstancePtr();
	mSystemMgr = SystemManager::Instance();
	mSoundMgr = SoundManager::Instance();

	logoSprite = new Sprite(TextureHelper::Instance()->GetTexture(TextureHelper::Logo),0,0,256,64);
	logoSprite->Scale(1.5f,1.5f);
	logoSprite->SetPosition(240,50);

	buttonSprite = new Sprite(TextureHelper::Instance()->GetTexture(TextureHelper::Utils),24,22,200,20);
	buttonSprite->SetPosition(240,150);

	sbuttonSprite = new Sprite(TextureHelper::Instance()->GetTexture(TextureHelper::Utils),24,42,200,20);
	sbuttonSprite->SetPosition(240,150);

	backSprite = new Sprite(TextureHelper::Instance()->GetTexture(TextureHelper::Dirt),0,0,16,16);
	backSprite->Scale(2,2);

	selectSaveSprite = new Sprite(TextureHelper::Instance()->GetTexture(TextureHelper::Glass),0,0,64,64);
	selectSaveSprite->Scale(7,0.6f);

	selectPos = 0;

	//load save info
	ScanSaveFiles("Save/");

	menuState = 0;
	loadSelectPos = 0;
	loadSavePos = 0;
	aboutPos = 0;

	//for map generation
	terrainType = 0;
	makeFlat = false;
	makeTrees = true;
	makeWater = true;
	makeCaves = true;
	generateSelectPose = 0;

	saveSubmenu = false;
	saveSubMenuSelect = 2;

	currentVersion = 30;

	//input helper
	InputHelper::Instance()->Init();
	InputHelper::Instance()->Load();
}

void StateMenu::Enter()
{
	mRender->SetOrtho(0,0,0,0,0,0);
	selectPos = 0;
}

void StateMenu::CleanUp()
{

}

void StateMenu::Pause()
{

}

void StateMenu::Resume()
{
	menuState = 0;
	loadSelectPos = 0;
	loadSavePos = 0;
	mRender->SetOrtho(0,0,0,0,0,0);
}

void StateMenu::HandleEvents(StateManager* sManager)
{
	//update input
	mSystemMgr->InputUpdate();

	switch(menuState)
	{
		case 0://main menu
		{
			//if triangle button pressed the exit
			if(mSystemMgr->KeyPressed(PSP_CTRL_START))
			{
				//running = false;
				sManager->Quit();
			}

			//up, down
			if(mSystemMgr->KeyPressed(PSP_CTRL_UP))
			{
				selectPos--;
				if(selectPos < 0)
					selectPos = 3;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_DOWN))
			{
				selectPos++;
				if(selectPos > 3)
					selectPos = 0;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CROSS))
			{
				if(selectPos == 0)//play state
				{
					menuState = 4;
					generateSelectPose = 0;
				}
				if(selectPos == 1)
				{

					ScanSaveFiles("Save/");

					menuState = 1;
					loadSelectPos = 0;
					loadSavePos = 0;
					loadSaveStart = 0;
					loadSaveEnd = saveFilesList.size();
					loadSaveMax = 4;
					if(loadSaveMax > loadSaveEnd)
						loadSaveMax = loadSaveEnd;
				}
				if(selectPos == 2)
				{
					//w teori opcje
					StateOptions *stateOptions = new StateOptions();
					stateOptions->Init();
					sManager->PushState(stateOptions);
				}
				if(selectPos == 3)
				{
					//about
					menuState = 3;
					aboutPos = 1;
				}
			}
		}
		break;
		case 1://load
		{
			if(saveSubmenu)
			{
				if(mSystemMgr->KeyPressed(PSP_CTRL_UP))
				{
					saveSubMenuSelect--;
					if(saveSubMenuSelect < 1)
						saveSubMenuSelect = 2;

					mSoundMgr->PlayMenuSound();
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_DOWN))
				{
					saveSubMenuSelect++;
					if(saveSubMenuSelect > 2)
						saveSubMenuSelect = 1;

					mSoundMgr->PlayMenuSound();
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_CROSS))
				{
					if(saveSubMenuSelect == 1)//remove file
					{
						//remove file
						if(saveFilesList.size() > 0)
						{
							if(saveFilesList[loadSavePos].compression)
							{
								//remove normal file
								remove(saveFilesList[loadSavePos].fileName.c_str());

								//remove compressed one
								saveFilesList[loadSavePos].fileName += "c";
								remove(saveFilesList[loadSavePos].fileName.c_str());

							}else
							{
								remove(saveFilesList[loadSavePos].fileName.c_str());
							}

							ScanSaveFiles("Save/");

							loadSelectPos = 0;
							loadSavePos = 0;
							loadSaveStart = 0;
							loadSaveEnd = saveFilesList.size();
							loadSaveMax = 4;
							if(loadSaveMax > loadSaveEnd)
								loadSaveMax = loadSaveEnd;

							saveSubmenu = false;
							saveSubMenuSelect = 2;
						}
					}
					if(saveSubMenuSelect == 2)//return
					{
						saveSubMenuSelect = 2;
						saveSubmenu = false;
					}
				}


			}else
			{
				if(mSystemMgr->KeyPressed(PSP_CTRL_UP))
				{
					loadSelectPos--;
					if(loadSelectPos < 0)
						loadSelectPos = 2;

					mSoundMgr->PlayMenuSound();
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_DOWN))
				{
					loadSelectPos++;
					if(loadSelectPos > 2)
						loadSelectPos = 0;

					mSoundMgr->PlayMenuSound();
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_LTRIGGER))
				{
					loadSavePos--;
					if(loadSaveEnd > 4)
					{
						loadSaveStart--;
						loadSaveMax--;

						if(loadSaveMax < 4)
						{
							loadSaveStart = 0;
							loadSaveMax = 4;
						}
					}

					if(loadSavePos < 0)
					{
						loadSavePos = saveFilesList.size() - 1;
						loadSaveMax = loadSaveEnd;
						loadSaveStart = loadSaveEnd - 4;
						if(loadSaveStart < 0)
							loadSaveStart = 0;
					}
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_RTRIGGER))
				{
					loadSavePos++;
					if(loadSavePos == loadSaveMax)
					{
						loadSaveStart++;
						loadSaveMax++;
						if(loadSaveMax > loadSaveEnd)
						{
							loadSaveStart = loadSaveEnd - 4;
							if(loadSaveStart < 0)
								loadSaveStart = 0;
							loadSaveMax = loadSaveEnd;
						}
					}
					if(loadSavePos >= saveFilesList.size())
					{
						loadSavePos = 0;
						loadSaveStart = 0;
						loadSaveMax = 4;
						if(loadSaveMax > loadSaveEnd)
							loadSaveMax = loadSaveEnd;
					}
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_CIRCLE))
				{
					menuState = 0;
				}

				if(mSystemMgr->KeyPressed(PSP_CTRL_CROSS))
				{
					if(loadSelectPos == 0)//play state
					{
						//load map
						if(saveFilesList.size() > 0)
						{
							StatePlay *statePlay = new StatePlay();
							LoadingScreen *loading = new LoadingScreen();
							statePlay->LoadMap(saveFilesList[loadSavePos].fileName,saveFilesList[loadSavePos].compression);
							statePlay->InitCamera();
							loading->KillLoadingScreen();
							delete loading;
							sManager->PushState(statePlay);
						}

					}
					if(loadSelectPos == 1)//daelete save
					{
						if(saveFilesList.size() > 0)
						{
							saveSubmenu = true;
							saveSubMenuSelect = 2;
						}
					}
					if(loadSelectPos == 2)
					{
						menuState = 0;

					}
				}
			}
		}
		break;
		case 3://about
		{
			if(mSystemMgr->KeyPressed(PSP_CTRL_UP))
			{
				aboutPos--;
				if(aboutPos < 0)
					aboutPos = 1;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_DOWN))
			{
				aboutPos++;
				if(aboutPos > 1)
					aboutPos = 0;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CROSS))
			{
				if(aboutPos == 1)
				{
					menuState = 0;
				}
				if(aboutPos == 0)
				{
					//checking for new update
					NetworkManager::Instance()->Init();
					//initialize connection
					if(NetworkManager::Instance()->InitializeConnection())
					{
						//show network dialog and connect to ap
						if(SystemManager::Instance()->ShowNetworkDialog())
						{
							//download update info
							if(NetworkManager::Instance()->GetFile("http://drakon.ixan.net/psp/version.txt","version.txt"))
							{
								//check what version is inside
								int fileVersion = 0;
								FILE * pFile;
								pFile = fopen("version.txt","r");
								if (pFile!=NULL)
								{
									char buffer[3];
									fread(buffer,1,3,pFile);
									fileVersion = atoi(buffer);
									fclose (pFile);

									if(fileVersion > currentVersion)
									{
										SystemManager::Instance()->ShowMessage("There is new version!!!");

										//ask question if user want to download updater
										if(SystemManager::Instance()->ShowMessageYesNo("Do you want to dowload LameCraft updater?") == 1)
										{
											//make new directory
											mkdir("ms0:/PSP/GAME/LameUpdater",777);
											//download updater there
											NetworkManager::Instance()->GetFile("http://drakon.ixan.net/psp/updater/EBOOT.PBP","ms0:/PSP/GAME/LameUpdater/EBOOT.PBP");
											//
											SystemManager::Instance()->ShowMessage("LameUpdater was installed.");
										}
									}else
									{
										SystemManager::Instance()->ShowMessage("You have latest version of the game.");
									}
								}else
								{
									SystemManager::Instance()->ShowMessage("Can't open version.txt .");
								}
							}else
							{
								SystemManager::Instance()->ShowMessage("Can't download update info.");
							}
						}else
						{
							SystemManager::Instance()->ShowMessage("You must connect to AP.");
						}
					}
					else
					{
						SystemManager::Instance()->ShowMessage("Can't initialize network.");
					}

					//turn off connection
					NetworkManager::Instance()->ShutDownConnection();
				}
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CIRCLE))
			{
				menuState = 0;
			}
		}
		break;
		case 4://chooose to generate randomly or marametric
		{
			if(mSystemMgr->KeyPressed(PSP_CTRL_UP))
			{
				generateSelectPose--;
				if(generateSelectPose < 0)
					generateSelectPose = 2;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_DOWN))
			{
				generateSelectPose++;
				if(generateSelectPose > 2)
					generateSelectPose = 0;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CIRCLE))
			{
				generateSelectPose = 0;
				menuState = 0;
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CROSS))
			{
				if(generateSelectPose == 0)
				{
					//ask for a name for new world
					char description[128];
					char starting[11] = {'W','o','r','l','d',' ','n','a','m','e','\0'};
					int lenght = 0;
					mRender->SetOrtho(0,0,0,0,0,0);
					if(mSystemMgr->ShowOSKDanzeff(starting,description,lenght) != -1)
					{
						std::string newWorldName = description;

						//push new state
						StatePlay *statePlay = new StatePlay();
						LoadingScreen *loading = new LoadingScreen();
						statePlay->Init();
						loading->KillLoadingScreen();
						delete loading;
						statePlay->InitCamera();
						statePlay->SetWorldAndSaveName(newWorldName,nextSaveFileName);
						sManager->PushState(statePlay);
					}
					menuState = 4;
					loadSelectPos = 0;
					loadSavePos = 0;
					selectPos = 0;
					generateSelectPose = 0;
				}

				if(generateSelectPose == 1)
				{
					generateSelectPose = 0;
					menuState = 5;

					terrainType = 0;
					makeFlat = false;
					makeTrees = true;
					makeWater = true;
					makeCaves = true;
				}

				if(generateSelectPose == 2)
				{
					generateSelectPose = 0;
					menuState = 0;
				}
			}
		}
		break;
		case 5://parametric terrain
		{
			if(mSystemMgr->KeyPressed(PSP_CTRL_UP))
			{
				generateSelectPose--;
				if(generateSelectPose < 0)
					generateSelectPose = 6;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_DOWN))
			{
				generateSelectPose++;
				if(generateSelectPose > 6)
					generateSelectPose = 0;

				mSoundMgr->PlayMenuSound();
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CIRCLE))
			{
				generateSelectPose = 0;
				menuState = 4;
			}

			if(mSystemMgr->KeyPressed(PSP_CTRL_CROSS))
			{
				if(generateSelectPose == 0)
				{
					//ask for a name for new world
					char description[128];
					char starting[11] = {'W','o','r','l','d',' ','n','a','m','e','\0'};
					int lenght = 0;
					mRender->SetOrtho(0,0,0,0,0,0);
					if(mSystemMgr->ShowOSKDanzeff(starting,description,lenght) != -1)
					{
						std::string newWorldName = description;

						//push new state
						StatePlay *statePlay = new StatePlay();
						LoadingScreen *loading = new LoadingScreen();
						statePlay->InitParametric(terrainType,makeFlat,makeTrees,makeWater,makeCaves);
						loading->KillLoadingScreen();
						delete loading;
						statePlay->InitCamera();
						statePlay->SetWorldAndSaveName(newWorldName,nextSaveFileName);
						sManager->PushState(statePlay);
					}
					menuState = 5;
					loadSelectPos = 0;
					loadSavePos = 0;
					selectPos = 0;
					generateSelectPose = 0;
				}

				if(generateSelectPose == 1)
				{
					terrainType++;

					if(terrainType > 2)
						terrainType = 0;
				}

				if(generateSelectPose == 2)
				{
					makeFlat = !makeFlat;

					if(makeFlat)
					{
						makeWater = false;
					}
				}

				if(generateSelectPose == 3)
				{
					makeTrees = !makeTrees;
				}

				if(generateSelectPose == 4)
				{
					//generate parametric terrain
					makeWater = !makeWater;

					//don't make water type terrain without hills
					if(makeWater)
					{
						makeFlat = false;
					}
				}

				if(generateSelectPose == 5)
				{
					makeCaves = !makeCaves;
				}

				if(generateSelectPose == 6)//go back
				{
					generateSelectPose = 0;
					menuState = 4;
				}
			}
		}
		break;
		case 6://check new version menu
		{

		}
		break;
	}

}

void StateMenu::Update(StateManager* sManager)
{

}

void StateMenu::Draw(StateManager* sManager)
{
	//start rendering
	mRender->StartFrame();

	switch(menuState)
	{
		case 0://main menu
		{
			sceGuDisable(GU_DEPTH_TEST);
			sceGuEnable(GU_BLEND);
			sceGuColor(GU_COLOR(1,1,1,1.0f));

			for(int x = 0;x < 16;x++)
			{
				for(int y = 0;y < 9;y++)
				{
					backSprite->SetPosition(x*32,y*32);
					backSprite->Draw();
				}
			}

			//logo
			logoSprite->Draw();

			//play
			buttonSprite->SetPosition(240,120);
			buttonSprite->Draw();

			//load
			buttonSprite->SetPosition(240,160);
			buttonSprite->Draw();

			//options
			buttonSprite->SetPosition(240,200);
			buttonSprite->Draw();

			//about
			buttonSprite->SetPosition(240,240);
			buttonSprite->Draw();

			//selected button
			sbuttonSprite->SetPosition(240,(selectPos * 40) + 120);
			sbuttonSprite->Draw();

			sceGuDisable(GU_BLEND);
			sceGuEnable(GU_DEPTH_TEST);

			//draw subtitles on buttons

			mRender->DebugPrint(240,125,"Play");
			mRender->DebugPrint(240,165,"Load");
			mRender->DebugPrint(240,205,"Options");
			mRender->DebugPrint(240,245,"About");
		}
		break;
		case 1://load menu
		{
			sceGuDisable(GU_DEPTH_TEST);
			sceGuEnable(GU_BLEND);
			sceGuColor(GU_COLOR(1,1,1,1.0f));

			for(int x = 0;x < 16;x++)
			{
				for(int y = 0;y < 9;y++)
				{
					backSprite->SetPosition(x*32,y*32);
					backSprite->Draw();
				}
			}

			//select sprite
			if(saveFilesList.size() > 0)
			{
				selectSaveSprite->SetPosition(240,50 + (loadSavePos * 40) - (loadSaveStart * 40));
				selectSaveSprite->Draw();
			}

			//save files
			for(int i = loadSaveStart;i <loadSaveMax;i++)
			{
				if(loadSavePos == i)
				{
					mRender->SetFontStyle(1.0f,0xFF000000,0xFFFFFFFF,0x00000000);
					mRender->DebugPrint(30,50 + (i * 40) - (loadSaveStart * 40),"%s",saveFilesList[i].worldName);

					mRender->SetFontStyle(0.7f,0xFF7F7F7F,0xFF000000,0x00000000);
					mRender->DebugPrint(40,65 + (i * 40) - (loadSaveStart * 40),"%s",saveFilesList[i].fileName.c_str());
				}else
				{
					mRender->SetFontStyle(0.8f,0xFFFFFFFF,0xFF000000,0x00000000);
					mRender->DebugPrint(30,50 + (i * 40) - (loadSaveStart * 40),"%s",saveFilesList[i].worldName);

					mRender->SetFontStyle(0.5f,0xFF7F7F7F,0xFF000000,0x00000000);
					mRender->DebugPrint(40,60 + (i * 40) - (loadSaveStart * 40),"%s",saveFilesList[i].fileName.c_str());
				}

			}

			//play
			buttonSprite->SetPosition(240,210);
			buttonSprite->Draw();

			//delete
			buttonSprite->SetPosition(240,235);
			buttonSprite->Draw();

			//back
			buttonSprite->SetPosition(240,260);
			buttonSprite->Draw();

			//selected button
			if(saveSubmenu)
			{
				sbuttonSprite->SetPosition(240,(saveSubMenuSelect * 25) + 210);
			}else
			{
				sbuttonSprite->SetPosition(240,(loadSelectPos * 25) + 210);
			}
			sbuttonSprite->Draw();

			sceGuDisable(GU_BLEND);
			sceGuEnable(GU_DEPTH_TEST);

			mRender->SetFontStyle(0.5f,0xFFFFFFFF,0xFF000000,0x00000200);
			//draw subtitles on buttons
			mRender->DebugPrint(240,20,"LOAD WORLD");

			if(saveSubmenu)
			{
				mRender->DebugPrint(240,215,"Are you sure?");
				mRender->DebugPrint(240,240,"Yes");
				mRender->DebugPrint(240,265,"No");
			}else
			{
				mRender->DebugPrint(240,215,"Play");
				mRender->DebugPrint(240,240,"Delete");
				mRender->DebugPrint(240,265,"Back");
			}
		}
		break;
		case 3://about
		{
			sceGuDisable(GU_DEPTH_TEST);
			sceGuEnable(GU_BLEND);
			sceGuColor(GU_COLOR(1,1,1,1.0f));

			for(int x = 0;x < 16;x++)
			{
				for(int y = 0;y < 9;y++)
				{
					backSprite->SetPosition(x*32,y*32);
					backSprite->Draw();
				}
			}

			//check for update
			buttonSprite->SetPosition(240,235);
			buttonSprite->Draw();

			buttonSprite->SetPosition(240,260);
			buttonSprite->Draw();

			//back
			sbuttonSprite->SetPosition(240,(aboutPos * 25) + 235);
			sbuttonSprite->Draw();

			sceGuDisable(GU_BLEND);
			sceGuEnable(GU_DEPTH_TEST);

			//about text
			mRender->SetFontStyle(0.7f,0xFFFFFFFF,0xFF000000,0x00000000);
			mRender->DebugPrint(40,100,"code:        Marcin Ploska(Drakon)");
			mRender->DebugPrint(40,120,"graphics:    Marcin Ploska(Drakon)");
			mRender->DebugPrint(40,140,"inspiration: Minecraft by Notch");
			mRender->DebugPrint(40,160,"www:        drakon.com.pl");
			mRender->DebugPrint(40,180,"rev:         op-30");

			//button text
			mRender->SetFontStyle(0.5f,0xFFFFFFFF,0xFF000000,0x00000200);
			mRender->DebugPrint(240,240,"Check for update");
			mRender->DebugPrint(240,265,"Back");
		}
		break;
		case 4://choose generation type
		{
			sceGuDisable(GU_DEPTH_TEST);
			sceGuEnable(GU_BLEND);
			sceGuColor(GU_COLOR(1,1,1,1.0f));

			for(int x = 0;x < 16;x++)
			{
				for(int y = 0;y < 9;y++)
				{
					backSprite->SetPosition(x*32,y*32);
					backSprite->Draw();
				}
			}

			//logo
			logoSprite->Draw();

			//Randomly
			buttonSprite->SetPosition(240,120);
			buttonSprite->Draw();

			//Parametric
			buttonSprite->SetPosition(240,160);
			buttonSprite->Draw();

			//back
			buttonSprite->SetPosition(240,200);
			buttonSprite->Draw();

			//selected button
			sbuttonSprite->SetPosition(240,(generateSelectPose * 40) + 120);
			sbuttonSprite->Draw();

			sceGuDisable(GU_BLEND);
			sceGuEnable(GU_DEPTH_TEST);

			//draw subtitles on buttons

			mRender->DebugPrint(240,125,"Random Terrain");
			mRender->DebugPrint(240,165,"Parametric Terrain");
			mRender->DebugPrint(240,205,"Back");
		}
		break;
		case 5://paramateric view
		{
			sceGuDisable(GU_DEPTH_TEST);
			sceGuEnable(GU_BLEND);
			sceGuColor(GU_COLOR(1,1,1,1.0f));

			for(int x = 0;x < 16;x++)
			{
				for(int y = 0;y < 9;y++)
				{
					backSprite->SetPosition(x*32,y*32);
					backSprite->Draw();
				}
			}

			//logo
			logoSprite->Draw();

			//generate
			buttonSprite->SetPosition(240,95);
			buttonSprite->Draw();

			//terrain type
			buttonSprite->SetPosition(240,120);
			buttonSprite->Draw();

			//flat/hills?
			buttonSprite->SetPosition(240,145);
			buttonSprite->Draw();

			//trees
			buttonSprite->SetPosition(240,170);
			buttonSprite->Draw();

			//water
			buttonSprite->SetPosition(240,195);
			buttonSprite->Draw();

			//caves
			buttonSprite->SetPosition(240,220);
			buttonSprite->Draw();

			//back
			buttonSprite->SetPosition(240,245);
			buttonSprite->Draw();

			//selected button
			sbuttonSprite->SetPosition(240,(generateSelectPose * 25) + 95);
			sbuttonSprite->Draw();

			sceGuDisable(GU_BLEND);
			sceGuEnable(GU_DEPTH_TEST);

			//draw subtitles on buttons
			mRender->DebugPrint(240,100,"Generate");

			if(terrainType ==0)
				mRender->DebugPrint(240,125,"Terrain type: Normal");
			else if(terrainType == 1)
				mRender->DebugPrint(240,125,"Terrain type: Winter");
			else if(terrainType == 2)
				mRender->DebugPrint(240,125,"Terrain type: Desert");

			makeFlat == true ?  mRender->DebugPrint(240,150,"Flat terrain"):mRender->DebugPrint(240,150,"Terrain with hills");
			makeTrees == true ? mRender->DebugPrint(240,175,"Trees : ON"):mRender->DebugPrint(240,175,"Trees : OFF");
			makeWater == true ? mRender->DebugPrint(240,200,"Water : ON"):mRender->DebugPrint(240,200,"Water : OFF");
			makeCaves == true ? mRender->DebugPrint(240,225,"Caves : ON"):mRender->DebugPrint(240,225,"Caves : OFF");

			mRender->DebugPrint(240,250,"Back");
		}
		break;
	}

	//draw debug text at the end
	/*mRender->DebugPrint(40,30,"cpu: %d%%",mRender->GetCpuUsage());
	mRender->DebugPrint(40,40,"gpu: %d%%",mRender->GetGpuUsage());
	mRender->DebugPrint(40,50,"saves: %d",saveFilesList.size());*/

	//mRender->SetFontStyle(0.5f,0xFFFFFFFF,0xFF000000,0x00000400);
	//mRender->DebugPrint(475,15,"CraftSite.pl Edition");
	//mRender->SetFontStyle(0.5f,0xFFFFFFFF,0xFF000000,0x00000200);

	//end frame
	mRender->EndFrame();
}

//additional functions
void StateMenu::ScanSaveFiles(const char* dirName)
{
	//clear list with save files
	saveFilesList.clear();

	DIR *Dir;
	struct dirent *DirEntry;
	Dir = opendir(dirName);

	while((DirEntry = readdir(Dir)) != NULL)
	{
	   if ( DirEntry->d_stat.st_attr & FIO_SO_IFREG)
	   {
		   SaveFile newSaveFile;
			std::string plik = dirName;
			plik += DirEntry->d_name;
			//dont load lmsc files
			size_t found = plik.find(".lmsc");
			size_t found2 = plik.find(".LMSc");
			if(found==std::string::npos && found2==std::string::npos)//nie znaleziono
			{
				newSaveFile.fileName = plik;
				saveFilesList.push_back(newSaveFile);
			}
	   }
	}

	closedir(Dir);

	/*//// this was causeing some problems
	int dfd;
	dfd = sceIoDopen(dirName);
	if(dfd > 0)
	{
		SceIoDirent dir;

		while(sceIoDread(dfd, &dir) > 0)
		{
			if(dir.d_stat.st_attr & FIO_SO_IFREG)//regular file
			{
				SaveFile newSaveFile;
				std::string plik = dirName;
				plik += dir.d_name;
				//dont load lmsc files
				size_t found = plik.find(".lmsc");
				if(found==string::npos)//nie znaleziono
				{
					newSaveFile.fileName = plik;
					saveFilesList.push_back(newSaveFile);
				}
			}
		}

		//close folder
		sceIoClose(dfd);
	}*/

	//now update all info in save files
	for(unsigned int i = 0;i < saveFilesList.size();i++)
	{
		FILE * pFile;
		pFile = fopen(saveFilesList[i].fileName.c_str(),"rb");

		if(pFile != NULL)
		{
			//version
			fread(&saveFilesList[i].saveVersion,sizeof(int),1,pFile);

			//name
			fread(saveFilesList[i].worldName,sizeof(char),50,pFile);

			if(saveFilesList[i].saveVersion >= 2)
				saveFilesList[i].compression = true;
			else
				saveFilesList[i].compression = false;

			fclose(pFile);
		}
	}

	//set next save name and numer
	nextSaveFileNumber = saveFilesList.size() + 1;
	{
		//ride thru all saves and check if i can use new name
		char liczba[3];
		sprintf(liczba,"%d",nextSaveFileNumber);
		nextSaveFileName = "Save/world";
		nextSaveFileName += liczba;
		nextSaveFileName += ".lms";

		for(unsigned int i = 0;i < saveFilesList.size();i++)
		{
			if(nextSaveFileName.compare(saveFilesList[i].fileName) == 0)
			{
				nextSaveFileNumber+=1;
				char liczba[3];
				sprintf(liczba,"%d",nextSaveFileNumber);
				nextSaveFileName = "Save/world";
				nextSaveFileName += liczba;
				nextSaveFileName += ".lms";
			}
		}
	}
}
